/**
 * Created with PyCharm.
 * User: yasin
 * Date: 13-8-16
 * Time: 下午10:31
 * To change this template use File | Settings | File Templates.
 */

W = {};
W.showMask = function(){
    var _mask = $(".mask");
    _mask.show();
    var pageHeight = $("body").height();

    _mask.height(pageHeight + "px");
};
W.hideMask = function(){
    var _mask = $(".mask");
    _mask.hide();
    _mask.height("");
};

W.showLogin = function(){
    W.showMask();
    $('#login_box').show();
};
W.showSign = function(){
    W.showMask();
    $('#sign_box').show();
};

W.hideLogin = function(){
    W.hideMask();
    $('#login_box').hide();
};
W.hideSign = function(){
    W.hideMask();
    $('#sign_box').hide();
    $('.co_title_login').text('');
};
W.hideEditBox = function(){
    W.hideMask();
    $('.edit_extend').remove();
    $('#edit_box').hide();

}
function chk_realname(realname){
    var reg = /^[\u4E00-\u9FA5]{1,4}$/;
    if (reg.test(realname)){
        return true;
    }else {
        return false;
    }
}

function chk_name(name_str){
    var reg = /^(\w|[\u4E00-\u9FA5]){2,20}$/;
    if(reg.test(name_str)){
        return true;
    }else{
        return false;
    }
}

function chk_email(email_str){
    //
    var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if(reg.test(email_str)){
        return true;
    }else{
        return false;
    }
}
function chk_num(str){
    var reg = /^\d{1,20}$/;
    if(reg.test(str)){
        return true;
    }else{
        return false;
    }
}
function chk_phone(str){
    var reg = /^1[3-8][0-9]{9}$/;
    if (reg.test(str)){
        return true;
    }else{
        return false;
    }
}
function chk_str(val){
    var reg = /^.{2,10}$/;
    if(reg.test(val)){
        return true;
    }else{
        return false;
    }
}

function chk_edit_form(){
    try{
        var text_len = $('#edit_form').find('input[name=text]').val().length;
    }catch(e){
        var text_len = 0;    
    }
    try{
        var url_len = $('#edit_form').find('input[name=url]').val().length;
    }catch(e){
        var url_len = 0;
    }
    try{
        var title_len = $('#edit_form').find('input[name=title]').val().length;
    }catch(e){
        var title_len = 0;
    }
    try{
        var alt_len = $('#edit_form').find('input[name=alt]').val().length;
    }catch(e){
        var alt_len = 0;
    }
    if(text_len>=20){
        alert('文字内容不能超过20个字');
        return false;
    }else if(url_len>=80){
        alert('超链接不能超过80个字符');
        return false;
    }else if (title_len>=20){
        alert('title不能超过20个字符');
        return false;
    }else if (alt_len>=20){
        alert('alt不能超过20个字符');
        return false;
    }

}


function chg_captcha(content, elem_id){
    var hidden_value = $(content).find('#'+elem_id).attr('value');
    var img_src = $(content).find('img.captcha').attr('src');
    $('#'+elem_id).attr('value', hidden_value);
    $('img.captcha').attr('src', img_src);
}

function block_pwd_btn(){
    var tleft = $('#t_left').text();
    if(tleft > 0){
        $('#t_left').text(tleft - 1);
        setTimeout(block_pwd_btn, 1000)
    }else{
        $('#t_left_span').remove();
        $('#send_btn').show();
        window.sending_email = false;
    }
}
window.sending_email = false;
function get_pwd(){
    var u_name = $('#get_pwd_input').val()
    if (u_name == ''){
        $('#next_time').text('请输入用户昵称/邮箱');
        return false;
    }else{
        if(!window.sending_email){
            window.sending_email = true;
            $.get('/ajax/send_mail_pwd/',{'username':u_name,t: new Date().getTime()},function(re){
                if(re == 1){
    //                $('#next_time').html('邮件已经发送稍后再次发送');
                    $('#send_btn').hide();
                    $('#send_btn_p').prepend('<span id="t_left_span">邮件已经发送，若未收到<a id="t_left">60</a>秒后可以重新发送</span>');
                    block_pwd_btn();
                }else if(re == 2){
                    $('#next_time').text('用户/邮箱不存在！');
                    window.sending_email = false;
                }else{
                    $('#next_time').text('提交出错！');
                    window.sending_email = false;
                }
            },'html');
        }
    }
    return false;
}

function qqlogin(){
    window.open('https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=100549508&redirect_uri=http://www.acscw.com/car/qqlogin/&scope=get_user_info,get_info',
            "getPwWindow",
            "height=490,width=600,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no,top=250px,left=450")

}
function wblogin(){
    window.open('https://api.weibo.com/oauth2/authorize?client_id=2879082738&response_type=code&redirect_uri=http://www.acscw.com/car/wblogin/',
    'getPwWindow',
    "height=490,width=600,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no,top=250px,left=450"
    );
}


$(function(){
//    $('.signup_django_form').find('a').click(function(){
//        var time_stamp = new Date().getTime();
//        $.get('/car/signup/',{captcha:1,t:time_stamp},function(e){
//            chg_captcha(e.content, 'id_captcha_0');
//        },'json')
//    });

    $('.CAPTCHA_a').click(function(){
        var time_stamp = new Date().getTime();
        var curr_url = '';
        var mark_id = $(this).parent().find('input').first().attr('id');
        if(mark_id == 'id_login_captcha_0'){
            curr_url = '/car/login/';
        }else if(mark_id == 'id_captcha_0'){
            curr_url = '/car/signup/';
        }else if(mark_id == 'id_post_captcha_0'){
            curr_url = '/car/postbar/';
        }
        $.get(curr_url,{captcha:1,t:time_stamp},function(e){
//            console.log(e.content);
            chg_captcha(e.content, mark_id);
        },'json');
        return false;
    });
//    $('.login_django_form').find('a').click(function(){
//        var time_stamp = new Date().getTime();
//        $.get('/car/login/',{captcha:1,t:time_stamp},function(e){
//            chg_captcha(e.content, 'id_login_captcha_0');
//        },'json')
//    });


    $('.close_window').click(function(){
            window.close();
    });

//    $(".mask").click(function(){
//        W.hideMask();
//        return false;
//    });
//$("body").click(function(){
//    W.showMask();
//    });
//
//$(".page").click(function(){
//    return false;
//    });
    $('#signup_form').ajaxForm({
        dataType:'json',
        success:function(c){
            if(c.success){
                if(c.msg == 0){
                    window.location.reload();
                }else{
                    alert('msg:', c.msg);
                }
            }else{
                if(c.msg == 11){
                    $('#cap_err').text('验证码错误');
                    chg_captcha(c.content, 'id_captcha_0');
                }else if(c.msg ==12){
                    if(c.content.search('email')==-1){
                        $('#id_username_err').text('此用户名已被占用');
                    }else if(c.content.search('name')==-1){
                        $('#id_email_err').text('此邮箱已被占用');
                    }
                    chg_captcha(c.content, 'id_captcha_0');
                }else{
                    alert('未知错误');
                }
            }
        },
        beforeSubmit:function(){
            var email_str = $('#id_email').val();
            if(!chk_email(email_str)){
                //check email
                $('#id_email_err').text('请输入正确的邮箱');
                return false;
            }else if(!chk_name($('#id_username').val(), 20)){
                $('#id_username_err').text('需2至20个汉字字母数字');
                return false;
            }else if($('#id_password').val().length<6){
                $('#id_password_err').text('密码需要六位以上');
                //console.log($('#is_agree').prop('checked'));
                return false;
            }
            else if($('#is_agree').prop('checked')==false){
                alert('请阅读协议');
                return false
            }
            else if ($('#id_password').val() != $('#id_repassword').val()){
                $('#id_repassword_err').text('两次密码输入不同');
                return false;
            }else if($('#id_captcha_1').val().length!=4){
                $('#cap_err').text('验证码错误');
                return false;
            }

            return true;
        },
        error:function(c, s, e){
            //console.log(e);
        }
    });
    $('#id_email').focus(function(){
        $('#id_email_err').text('');
    });
    $('#id_username').focus(function(){
        $('#id_username_err').text('');
    });
    $('#id_password').focus(function(){
        $('#id_password_err').text('');
    });
    $('#id_repassword').focus(function(){
        $('#id_repassword_err').text('');
    });
    $('#id_captcha_1').focus(function(){
        $('#cap_err').text('');
    });

    $('#get_pwd_input').focus(function(){
        $('#next_time').text('');
    });

    $('#login_form').ajaxForm({
        dataType:'json',
        beforeSubmit:function(){
            if($('#id_login_captcha_1').val().length!=4){
                $('.co_title_login').text('验证码错误');
                return false;
            }
            return true;
        },
        success:function(c){
            if(c.success){
                if(c.msg == 0){
                    window.location.reload();
                }else{
                    alert('msg:', c.msg);
                }
            }else{
                if(c.msg == 21){
                    $('.co_title_login').text('用户名或密码错误');
                    chg_captcha(c.content, 'id_login_captcha_0');
                }else if(c.msg == 22){
                    //invalid error
                    $('.co_title_login').text('您的验证码输入有误');
                    chg_captcha(c.content, 'id_login_captcha_0');
                }
            }
        },
        error:function(c, s, e){
//            console.log(e);
        }
    });

    $('.menu-btn').click(function(e){

        if($(e.target).attr('class') == 'menu-href'){
            //
            var div_id = $(e.target).parent().attr('id');
        }else{
            //
            var div_id = $(e.target).attr('id');
        }

        if(div_id == 'home'){
            window.location.href = '/';
        }else if(div_id == 'joined'){
            window.location.href = '/car/search/';
        }else if(div_id == 'contacts'){
            window.location.href = '/car/contacts';
        }else if(div_id == 'recruitment'){
            window.location.href = '/car/recruitment/';
        }else if(div_id == 'postbar'){
            window.location.href = '/car/postbar/';
        }else if(div_id == 'news'){
            window.location.href = '/car/news/';
        }else if(div_id == 'case'){
            window.location.href = '/car/case/';
        }else if(div_id == 'consult'){
            window.location.href = '/car/consult/';
        }

    });

    $('.cms_edit').click(function(){
        W.showMask();
        $('#edit_box').show();
        var which_elem = $(this).attr('elem_id');
        var elem_type = $(this).attr('elem_type');
        if(window.shop_id){
            $('#which_shop').val(window.shop_id);
        }else{
            $('#which_shop').val(0);
        }
        $('#which_elem').val(which_elem);
        if(elem_type == 1){
            $('#edit_form').prepend(image_inputs.innerHTML);
            var href_str = 'http://';
            if ($(this).attr('href')!='#'){
                href_str = $(this).attr('href');
            }
            $('#edit_form').find('input:first').val(href_str);
            $('#edit_form').find('input:eq(2)').val($(this).find('img').attr('alt'));
            $('#edit_form').find('input:eq(3)').val($(this).attr('title'));

            $('#edit_form').find('#img_height').text($(this).find('img').attr('height'));
            $('#edit_form').find('#img_width').text($(this).find('img').attr('width'));
        }else if(elem_type == 2){
            var href_str = 'http://';
            if ($(this).attr('href')!='#'){
                href_str = $(this).attr('href');
            }
            $('#edit_form').prepend(link_inputs.innerHTML);
            $('#edit_form').find('input:first').val(href_str);
            $('#edit_form').find('input:eq(1)').val($(this).text());
            $('#edit_form').find('input:eq(2)').val($(this).attr('title'));
        }else if(elem_type == 4){
            $('#edit_form').prepend(label_inputs.innerHTML).find('input:first').val($(this).text());
        }
        return false;
    });

//    $('label[for=id_captcha_1]').css({'color':'#666'});
//    $('label[for=id_login_captcha_1]').css({'color':'#666'});
    $('label[for=id_login_captcha_1]').addClass('control-label ctrl-small');
    if(window.is_label_normal){
        //
        $('label[for=id_post_captcha_1]').addClass('control-label');
    }else{
        $('label[for=id_post_captcha_1]').addClass('control-label ctrl-smalls');
    }
    $('label[for=id_captcha_1]').addClass('control-label');
    $('label[for=is_agree]').css({'color': '#9a9a9a','font-size':'12px'});

    $('.mask').click(function(){
        W.hideLogin();
        W.hideSign();
        W.hideEditBox();
        $('#get_pwd_box').hide();
    });
    $('.box_close').click(function(){
        W.hideLogin();
        W.hideSign();
        W.hideEditBox();
        $('#get_pwd_box').hide();
    });
    $('#signup_imd').click(function(){
        W.hideLogin();
        W.showSign();
    });
    $('#fogetpwd').click(function(){
        W.hideLogin();
        W.showMask();
       $('#get_pwd_box').show();
    });

    ///////////////////////////////////////////
    $('.qrcode').hover(function(){
        $(this).find('a').css({'color':'#666'});
    }, function(){
        $(this).find('a').css({'color':'#06c'});
    });

    $('#login_form').find('input').click(function(){
        $('.co_title_login').text('');
    });

    $('.login_box_btn').click(function(){
        W.showLogin();
        return false;
    });

    $('.sign_box_btn').click(function(){
        W.showSign();
        return false;
    });

});

